// <copyright file="MenuController.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Web.Cms.Admin.Controllers
{
    using System.Web.Mvc;
    using System.Collections.Generic;
    using System;

    using CleverCMS.Core.Extensions;
    using CleverCMS.Cms.Data.Entities;
    using CleverCMS.Cms.Services;

    [Authorize(Roles = "Admin")]
    public class MenuController : Controller
    {
        private readonly MenuService m_menuService;

        public MenuController(MenuService menuService)
        {
            this.m_menuService = menuService;
        }

        public ActionResult List()
        {
            return View(this.m_menuService.LoadMenues());
        }

        
        
        
        public ViewResult Edit(int? id)
        {
            //if (null != id)
            //{
            //    CmsMenu menu = this.menuService.LoadMenu(id.Value);
            //    return View(menu.MenuItems);
            //}

            return View();
        }
        

       
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection forms)
        {
            if (id == 0)
            {
                CmsMenu newMenu = new CmsMenu()
                                   {
                                       Title = forms.GetValue("Title").AttemptedValue,
                                       Description = forms.GetValue("Description").AttemptedValue,
                                       Type = forms.GetValue("Type").AttemptedValue
                                   };

                return this.CreateNewMenu(newMenu);
            }

            CmsMenu menu = this.m_menuService.LoadMenu(id);
            UpdateModel(menu, forms.ToValueProvider());
            this.m_menuService.SaveMenu(menu);
            
            TempData["SuccessResult"] = "Menu was successfully saved";
            return this.View(menu);
        }

        private ActionResult CreateNewMenu(CmsMenu menu)
        {
            this.m_menuService.SaveMenu(menu);
            TempData["SuccessResult"] = string.Format("Menu was successfully created with Id = {0}", menu.Id);
            return RedirectToAction("Edit", "Menu", new { id = menu.Id });
        }
    }
}